package com.interview.a_多线程.c_停止线程.e_停止线程_错误方法.b_volatile_标记问题.a_看似可行;

/**
 * volatile 关键字，多个线程之间都可以看到 它声明的值
 * 看似可行
 */

public class StopThread_1 implements Runnable {
    private volatile boolean isCancel = false;

    public static void main(String[] args) throws InterruptedException {
        StopThread_1 stopThread_1 = new StopThread_1();
        Thread thread = new Thread(stopThread_1);
        thread.start();

        //执行0.5秒时中断
        Thread.sleep(1200);
        stopThread_1.isCancel = true;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            if (isCancel) {
                System.out.println("结束并记录日志");
                break;
            }
            System.out.println(i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
